<template>
	<view class="content">
		<!-- #ifdef APP-PLUS -->
		<view class="status_bar"><view class="top_view"></view></view>
		<!-- #endif -->
		<view class="index">
			<view class="index_header_main">
				<view class="index_header">
					<view class="header_title">明早打卡可随机瓜分金额({{ CODECONFIG.signBaseCode }})</view>
					<view class="header_money">
						<text class="money">{{ poolAsset }}</text>
						<text class="coin">{{ CODECONFIG.signBaseCode }}</text>
					</view>
					<view class="header_participate">
						<text>当前参与人数：{{ joinNum }}</text>
					</view>
					<view class="header_success">
						<text>
							昨日报名：
							<text style="color: #ff6100;">{{ allNum }}</text>
						</text>
						<text style="margin-left: 20upx;">
							打卡成功：
							<text style="color: #ff6100;">{{ sucNum }}</text>
						</text>
					</view>
				</view>
				<view class="index_rule">
					<view class="rule" @click="goPage('../rule/rule')">
						<text>规则</text>
						<text>介绍</text>
					</view>
					<view class="invitation" style="margin-top: 16upx;" @click="goPage('../invitation/invitation')">
						<text>邀请</text>
						<text>好友</text>
					</view>
				</view>
				<view class="index_submits" v-if="!loading">
					<button v-if="isFlag && userFlag == 'NO'" @click="submits(true)">早起打卡</button>
					<button class="noS" v-else-if="userFlag == 'YES' && !isPrize">红包生成中~预计8点完成派发</button>
					<button v-else-if="dayPrize * 1 <= 0 && isPrize && userFlag == 'YES' && userPrize == 'NO'" @click="getProfit()">领取打卡红包</button>
					<button v-else-if="!isFlag && isTime && signInFlag == 'NO'" @click="submits(false)">
						支付 {{ CODECONFIG.signCostAsset }} {{ CODECONFIG.signBaseCode }}参与挑战
					</button>
					<button class="noS" v-else-if="signInFlag == 'YES'">等待打卡</button>
					<button class="noS" v-else-if="!isFlag && !isTime">参与或打卡时间未到</button>
					<button class="noS" v-else-if="isJoin">您未参与活动无法进行早起打卡</button>
					<!-- <text>{{isFlag}},{{isJoin}},{{isTime}},{{signInFlag}},{{dayPrize}},{{isPrize}},{{userFlag}},{{userPrize}}</text> -->
				</view>
				<view class="index_submits" v-else><button class="noS">加载中...</button></view>
				<view class="index_tips clear">
					<text class="fl">报名时间：8:00-23:30</text>
					<text class="fr">打卡时间：5:00-7:30(次日)</text>
				</view>
				<view class="index_prize clear">
					<view class="fl">
						<text class="number">{{ dayPrize }}</text>
						<text class="text">今日打卡红包({{ CODECONFIG.signBaseCode }})</text>
						<!-- <text class="see">查看</text> -->
					</view>
					<view class="fr">
						<text class="number">{{ monthAsset }}</text>
						<text class="text">当月奖池({{ CODECONFIG.signBaseCode }})</text>
					</view>
				</view>
			</view>
			<view class="index_list">
				<view class="list_nav clear">
					<view class="fl" :class="type == 'RANK' ? 'active' : ''" @click="type = 'RANK'">
						<text class="text">今日打卡榜</text>
						<text class="border"></text>
					</view>
					<view class="fr" :class="type == 'INVIT' ? 'active' : ''" @click="type = 'INVIT'">
						<text class="text">好友邀请榜</text>
						<text class="border"></text>
					</view>
				</view>
				<view class="list_main">
					<template v-if="type == 'RANK'">
						<view class="item" v-for="(item, index) in rankList" :key="index" v-if="item.uid != 0">
							<view class="item_img_number">
								<template v-if="index < 3">
									<image v-if="index == 0" src="/static/index/l1.gif" mode=""></image>
									<image v-if="index == 1" src="/static/index/l2.gif" mode=""></image>
									<image v-if="index == 2" src="/static/index/l3.gif" mode=""></image>
								</template>
								<template v-else>
									<view style="font-family: '黑体'; font-size: 34upx; font-weight: bold; color: #717171;">{{ index + 1 }}</view>
								</template>
							</view>
							<view class="item_account">
								{{ REG_EMAIL.test(item.account) ? filter.email(item.account) : filter.getSuffixPhone(item.account) }} ({{ filter.getFormattedUid(item.uid) }})
							</view>
							<view class="item_profit" style="max-width: 150rpx;">
								<!-- <text class="text">{{item.invest}}</text> -->
								<text class="text">{{ $formta(item.signTime, 'hh:mm:ss') }}</text>
								<!-- <text class="coin">{{$formta(item.signTime,'hh:mm')}}</text> -->
							</view>
						</view>
						<uni-load-more :status="loading ? 'loading' : !rankList[0] ? 'noMore' : 'noMore'"></uni-load-more>
					</template>
					<template v-else>
						<view class="item" v-for="(item, index) in invitList" :key="index">
							<view class="item_img_number">
								<template v-if="index < 3">
									<image v-if="index == 0" src="/static/index/l1.gif" mode=""></image>
									<image v-if="index == 1" src="/static/index/l2.gif" mode=""></image>
									<image v-if="index == 2" src="/static/index/l3.gif" mode=""></image>
								</template>
								<template v-else>
									<view style="font-family: '黑体'; font-size: 34upx; font-weight: bold; color: #717171;">{{ index + 1 }}</view>
								</template>
							</view>
							<view class="item_account">{{ REG_EMAIL.test(item.account) ? filter.email(item.account) : filter.getSuffixPhone(item.account) }}</view>
							<view class="item_profit">
								<!-- <text class="text">{{item.invest}}</text> -->
								<text class="text">{{ item.inviteNum }}</text>
								<text class="coin">人</text>
							</view>
						</view>
						<uni-load-more :status="loading ? 'loading' : !invitList[0] ? 'noMore' : 'noMore'"></uni-load-more>
					</template>
				</view>
			</view>
		</view>
		<HMmessages ref="HMmessages" @complete="HMmessages = $refs.HMmessages" @clickMessage="clickMessage"></HMmessages>
		<Modal :title="`是否支付${CODECONFIG.signCostAsset} ${CODECONFIG.signBaseCode}参与挑战？`" v-if="isPay" @close="closeModal('isPay')">
			<view class="common_model">
				<view class="common_form" style="padding-bottom: 0;">
					<view class="item clear" style="margin-bottom: 0;">
						<view class="fr" style="width: 100%;">
							<picker @change="setCode" :value="codeValue" :range="list" :range-key="'assetCode'">
								<view class="uni-input">支付币种: {{ list[codeValue] ? list[codeValue].assetCode : '' }}</view>
							</picker>
							<view class="downs"><uni-icon type="arrowdown" size="20" :color="'#ff6100'"></uni-icon></view>
						</view>
					</view>
					<view class="tips clear">
						<text class="fl">{{ CODECONFIG.signCostAsset + ' ' + CODECONFIG.signBaseCode }} ≈ {{ rmbCode(CODECONFIG.signBaseCode)}} cny</text>
					</view>
					<view class="item clear" style="margin-bottom: 0;">
						<view class="fr" style="width: 100%;"><input type="password" v-model="payPwd" placeholder="请输入资金密码" /></view>
					</view>
				</view>
				<view style="color: #000; margin-bottom: 20rpx;">(请以实际扣款为准)</view>
				<view class="common_btn">
					<button class="active" @click="submits(true)">确定</button>
					<button @click="closeModal('isPay')">取消</button>
				</view>
			</view>
		</Modal>
	</view>
</template>

<script>
import service from './service.js';
import HMmessages from '@/components/HM-messages/HM-messages.vue';
import Modal from '@/components/winmax-modal/index.vue';
import filter from '@/common/filter.js';
import {REG_EMAIL} from '@/common/reg.js';
import comSvc from '@/common/comSvc.js';
import {
  uniLoadMore,
  uniIcon
} from '@dcloudio/uni-ui';
import { mapGetters } from 'vuex'
export default {
  components: {
    HMmessages,
    Modal,
    uniLoadMore,
    uniIcon
  },
  computed: {
    ...mapGetters(['COINRMB','CODECONFIG'])
  },
  data() {
    return {
      REG_EMAIL,
      filter,
      type:'RANK',
      rankList: [],
      invitList:[],
      isPay: false,
      payPwd: '',
      actID: '',
      signInFlag: 'NO',
      joinNum: 0,
      poolAsset: 0,
      monthAsset: 0,
      dayPrize: 0,
      Time: [0, 0],
      isFlag: false,
      isJoin: true,
      isPrize: false,
      loading: true,
      isTime: false,
      userFlag: 'NO',
      userPrize:'NO',
      allNum:0,
      sucNum:0,
      codeValue:0,
      list:[],
    };
  },
  onLoad(option) {
    if (option && option.token) {
      uni.setStorageSync('TOKEN', option.token);
      uni.setStorageSync('LOGIN_TIME', option.time);
    } else {
      uni.clearStorageSync();
    }
    uni.setStorageSync('isAppH5', option.isAppH5 ? true : false);
  },
  // onPullDownRefresh() {
  //   this.inits();
  // },
  onShow() {
    this.Time = this.$formta(new Date(), 'hh:mm').split(':');
    // 早起打卡时间
    if (this.Time[0] * 1 > 4 && (this.Time[0] * 1 < 7 || this.Time[0] * 1 == 7)) {
      if (this.Time[0] * 1 == 7 && (this.Time[1] * 1 < 30 || this.Time[1] * 1 == 30)) {
        this.isFlag = true;
      } else if (this.Time[0] * 1 < 7) {
        this.isFlag = true;
      } else {
        this.isFlag = false;
      }
    } else {
      this.isFlag = false;
    }
    //参与时间
    if (this.Time[0] * 1 > 7 && (this.Time[0] * 1 < 23 || this.Time[0] * 1 == 23)) {
      if (this.Time[0] * 1 == 23 && (this.Time[1] * 1 < 30 || this.Time[1] * 1 == 30)) {
        this.isTime = true;
      } else if (this.Time[0] * 1 < 23) {
        this.isTime = true;
      } else {
        this.isTime = false;
      }
    } else {
      this.isTime = false;
    }
    //领红包时间
    if (this.Time[0] * 1 > 7 && (this.Time[0] * 1 < 22 || this.Time[0] * 1 == 22)) {
      if (this.Time[0] * 1 == 22 && (this.Time[1] * 1 < 59 || this.Time[1] * 1 == 59)) {
        this.isPrize = true;
      } else if (this.Time[0] * 1 < 22) {
        this.isPrize = true;
      } else {
        this.isPrize = false;
      }
    } else {
      this.isPrize = false;
    }
    this.inits();
  },
  methods: {
    //返回对应的RMB
    rmbCode(code){
      let money = 0;
      for(let i of this.list){
        if(i.assetCode == code){
          money = filter.fix((i.RMB * this.CODECONFIG.signCostAsset),6);
          break;
        }
      }
      return money;
    },
    //根据配置列表或者对应价格
    getMoney() {
      comSvc.getTrade().then(({data})=>{
        let array = [];
        let USDKNum = 0;
        let convertCodeList = [this.CODECONFIG.signBaseCode,...this.CODECONFIG.convertCode]
        for (let obj of convertCodeList) {
          let i = {
            assetCode:obj
          }
          i.USDK = 0;
          i.RMB = 0;
          if (data[`USDK_${i.assetCode}`]) {
            i.USDK = data[`USDK_${i.assetCode}`].newPrice || 0;
          } else if (i.assetCode == 'USDK') {
            i.USDK = 1;
          } else if (i.assetCode == 'UDC') {
            i.USDK = 1;
          } else if (i.assetCode == 'DUSD') {
            i.USDK = 1;
          } else if (data[`UDC_${i.assetCode}`]) {
            i.USDK = data[`UDC_${i.assetCode}`].newPrice || 0;
          } else if (data[`DUSD_${i.assetCode}`]) {
            i.USDK = data[`DUSD_${i.assetCode}`].newPrice || 0;
          }
          i.USDK = filter.fix(i.USDK, 6);
          if (i.assetCode == 'UDC' || data[`UDC_${i.assetCode}`]) {
            i.RMB = 6.58 * i.USDK;
          } else if (i.assetCode == 'DUSD' || data[`DUSD_${i.assetCode}`]) {
            i.RMB = this.COINRMB.digitalusd * i.USDK;
          } else {
            i.RMB = parseFloat(this.COINRMB.tether) * i.USDK;
			console.log(i.RMB)
          }
          i.RMB = filter.fix(i.RMB, 6);
          array.push(i);
        }
        this.list = array;
  console.log(this.list)
      })
    },
    //设置支付币种
    setCode(e){
      this.codeValue = e.target.value;
    },
    //获取邀请排行榜
    getInvitList(){
      this.loading = true;
      service.getInvitList().then(({data})=>{
        this.loading = false;
        if(data.code == '100200'){
          this.invitList = data.data;
        }else{
          this.errors(data.msg);
        }
      })
    },
    //获取成功打卡人数
    getUserNumber(){
      service.getUserNumber().then(({data})=>{
        if(data.code == '100200'){
          this.allNum = data.data.ALL_NUM;
          this.sucNum = data.data.SUCCESS_NUM;
        }else{
          this.errors(data.msg);
        }
      })
    },
    //领红包标记
    profitFlag(){
      service.profitFlag().then(({data})=>{
        if(data.code == '100200'){
          this.userPrize = data.data.profit;
        }else{
          this.errors(data.msg);
        }
      })
    },
    //打卡标记
    SignFlag() {
      service.SignFlag().then(({
        data
      }) => {
        if (data.code == '100200') {
          this.userFlag = data.data.signup;
        } else {
          this.errors(data.msg);
        }
      })
    },
    //领取打卡红包
    getProfit() {
      this.$api.showLoading();
      service.getProfit().then(({
        data
      }) => {
        if (data.code == '100200') {
          this.inits();
          this.HMmessages.show('领取红包成功', {
            textAlign: 'center',
            background: '#356e36',
            fontColor: '#fff'
          });
        } else {
          this.$api.hideLoading();
          this.errors(data.msg);
        }
      })
    },
    //初始化
    inits() {
      this.rankList = [];
      this.invitList = [];
      this.$api.showLoading();
      this.getMoney();
      service.init().then(({
        data
      }) => {
        this.$api.hideLoading()
        if (data.code == '100200') {
          this.actID = data.data;
          this.getDay();
          this.getMonth();
          this.getRank();
          this.getUserNumber();
          this.getInvitList();
          if (this.$api.isLogin()) {
            this.getCurrentProfit();
            this.getSignDay();
            this.SignFlag();
            this.profitFlag();
          }
        } else {
          this.errors(data.msg);
        }
      });
    },
    //查询打卡排行榜
    getRank() {
      this.loading = true;
      service.getRank().then(({
        data
      }) => {
        this.loading = false;
        if (data.code == '100200') {
          this.rankList = data.data.reverse();
        } else {
          this.errors(data.msg);
        }
      });
    },
    //查询今日红包
    getCurrentProfit() {
      service.getCurrentProfit().then(({
        data
      }) => {
        if (data.code == '100200') {
          this.dayPrize = data.data && data.data.allotProfit ? data.data.allotProfit : 0;
        } else {
          this.errors(data.msg);
        }
      });
    },
    //查询当月奖池
    getMonth() {
      service.getMonth().then(({
        data
      }) => {
        if (data.code == '100200') {
          this.monthAsset = data.data.poolAsset;
        } else {
          this.errors(data.msg);
        }
      });
    },
    //查询今日奖池和数据
    getDay() {
      service.getDay().then(({
        data
      }) => {
        if (data.code == '100200') {
          this.joinNum = data.data.joinNum;
          this.poolAsset = data.data.poolAsset;
        } else {
          this.errors(data.msg);
        }
      });
    },
    //报名标记
    getSignDay() {
      service.SignDay().then(({
        data
      }) => {
        if (data.code == '100200') {
          this.signInFlag = data.data.signin;
          this.isJoin = data.data.signin == 'NO' ? true : false;
        } else {
          this.errors(data.msg);
        }
      });
    },
    //打卡
    submits(bool) {
      if (!this.$api.isLogin()) {
        uni.showModal({
          title: '提示',
          content: '参与挑战请先进行登录'
        });
        return false;
      }
      if (!bool) {
        this.isPay = true;
        return false;
      }
      this.$api.showLoading();
      if (!this.isFlag) {
        let params = {
          id: this.actID
        }
        if(this.CODECONFIG.signBaseCode != this.list[this.codeValue].assetCode){
          params.assetCode = this.list[this.codeValue].assetCode;
        }
        //参与活动
        service.Sign(params, {
          'pay-password': this.payPwd
        }).then(({
          data
        }) => {
          this.$api.hideLoading();
          if (data.code == '100200') {
            this.closeModal('isPay');
            this.inits();
            this.HMmessages.show('参与成功', {
              textAlign: 'center',
              background: '#356e36',
              fontColor: '#fff'
            });
          } else {
            this.errors(data.msg);
          }
        });
      } else {
        //打卡
        service.SignIn({
          id: this.actID
        }).then(({
          data
        }) => {
          this.$api.hideLoading();
          if (data.code == '100200') {
            this.inits();
            this.HMmessages.show('打卡成功', {
              textAlign: 'center',
              background: '#356e36',
              fontColor: '#fff'
            });
          } else {
            this.errors(data.msg);
          }
        });
      }
    },
    //前往页面
    goPage(path) {
      uni.navigateTo({
        url: path
      });
    },
    //关闭弹窗
    closeModal(name) {
      this[name] = false;
    },
    //错误提示
    errors(text) {
      this.HMmessages.show(text, {
        textAlign: 'center',
        background: '#74272d',
        fontColor: '#fff'
      });
    }
  }
};
</script>

<style lang="scss">
@import './style.scss';
</style>
